home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / time / dclock16.lha / DClock16 / src / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-05  |  22.2 KB  |  943 lines

  1. #include <exec/types.h>
  2. #include <intuition/intuition.h>
  3. #include <devices/timer.h>
  4. #include <workbench/startup.h>
  5. #include <dos/dos.h>
  6. #include <dos/dostags.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/gadtools_protos.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/alib_protos.h>
  12. #include <clib/wb_protos.h>
  13. #include <clib/icon_protos.h>
  14. #include <graphics/gfxbase.h>
  15. #include <reqtools/reqtools.h>
  16. #include <workbench/startup.h>
  17. #include <workbench/workbench.h> 
  18. #include <stdio.h> 
  19. #include <string.h>
  20.  
  21. #include "Window.h"
  22. #include "MenuDefs.h"
  23. #include "Main.h"
  24. #include "Draw.h"
  25. #include "SetAlarm.h"
  26.  
  27.  
  28. #define HANDLE_DONE 0
  29. #define HANDLE_DRAGBAR 1
  30. #define HANDLE_DATE 2
  31. #define HANDLE_ICONIFY 3
  32.  
  33. // Prototypes ---------------------------------------------------------------
  34.  
  35. void wbmain(struct WBStartup *ws);
  36. void main(int argc, char **argv);
  37. int  HandleEvents(void);
  38. BOOL OpenAllLibs(void);
  39. void CloseAllLibs(void);
  40. void About(void);
  41. BOOL LoadPreferences(struct AppPrefs *prefs);
  42. BOOL SavePreferences(struct AppPrefs *prefs);
  43. BOOL OpenTimerDevice(void);
  44. void CloseTimerDevice(void);
  45. void SetTimer(ULONG delay);
  46. int  Timeval2Seconds(int timeval);
  47. int  Seconds2Timeval(int secs);
  48. BOOL IsChecked(int menuNumber);
  49. void DClockTask(void);
  50. BOOL AllocateParentSignals(void);
  51. void FreeParentSignals(void);
  52. BOOL AllocateChildSignals(void);
  53. void FreeChildSignals(void);
  54. void InfoTask(void);
  55. BOOL AllocateInfoSignals(void);
  56. void FreeInfoSignals(void);
  57. void SetCommand(int type);
  58. int  myArgInt(char **argv, char *tool, int dfault);
  59. char *myArgString(char **argv, char *tool, char *dfault);
  60. BOOL myArgBool(char **argv, char *tool, BOOL dfault);
  61. BOOL DoAppIcon(void);
  62.  
  63. // Statics ------------------------------------------------------------------
  64.  
  65. UBYTE *vers = "\0$VER: DClock V1.6 (c) 1995";
  66. char PrefsName[] = "ENVARC:DClock.prefs";
  67.  
  68. struct IntuitionBase *IntuitionBase = NULL;
  69. struct Library *GadToolsBase = NULL;
  70. struct GfxBase *GfxBase = NULL;
  71. struct ReqToolsBase *ReqToolsBase = NULL;
  72. struct Library *IconBase = NULL;
  73. struct Library *WorkbenchBase = NULL;
  74.  
  75. struct AppPrefs myPrefs =
  76. {
  77.     1,      // foreground color
  78.     2,      // background color
  79.     200,    // window position x
  80.     10,     // window position y
  81.     TRUE,   // display seconds: true/false
  82.     FALSE,  // alarm on
  83.     FALSE,  // timer on
  84.     TRUE,   // dragbar
  85.     0,      // alarmval
  86.     0,      // timerval
  87.     TRUE,   // twentyfour
  88.     TRUE,   // date
  89.     FALSE,  // activate
  90.     TRUE    // date format dd-mm-yy
  91. };
  92.  
  93. struct SignalSemaphore draw_semaphore = {0};
  94. struct timerequest *TimerIO = NULL;
  95. struct MsgPort *TimerMP = NULL;
  96. struct Message *TimerMSG = NULL;
  97. ULONG timerError = 1;
  98.  
  99. ULONG alarmsig, timersig, cleanupsig, exitsig, exit_info, info_done, alarmcmd, timercmd;
  100. LONG alarmsignum, timersignum, cleanupsignum, exitsignum, exit_info_num, info_done_num, alarmcmdnum, timercmdnum;
  101.  
  102. struct Task *childtask;
  103. struct Task *parenttask;
  104. struct Task *infotask;
  105.  
  106. FILE *conwin = NULL; 
  107. char *PubScreenName = NULL;
  108.  
  109. struct TagItem ezRequestTags[] =
  110. {  RT_PubScrName, (ULONG)PubScreenName,
  111.     TAG_DONE, 0
  112. };
  113.  
  114. char DiskObjectName[256] = {0};  // Name of .info file
  115.  
  116. // Main function ------------------------------------------------------------
  117.  
  118. void wbmain(struct WBStartup *ws)
  119. {
  120.     struct DiskObject *dobj;
  121.     int i = 0, argc = 1;
  122.     struct WBArg *wbarg;
  123.     LONG olddir = -1;
  124.     char args[14][40];
  125.     char *argv[14];
  126.     char **toolarray;
  127.  
  128.  
  129.     if (conwin = fopen("CON:10/10/540/200/Output/auto/close", "r+"))
  130.     {
  131.         if (IconBase = OpenLibrary("icon.library", 36L))
  132.         {
  133.             wbarg = ws->sm_ArgList;
  134.  
  135.             olddir = -1;
  136.             if ((wbarg->wa_Lock) && (*wbarg->wa_Name))
  137.                 olddir = CurrentDir(wbarg->wa_Lock);
  138.  
  139.             strcpy(args[0], wbarg->wa_Name);
  140.             dobj=(struct DiskObject *)GetDiskObject((UBYTE *)wbarg->wa_Name);
  141.  
  142.             if((*wbarg->wa_Name) && (dobj))
  143.             {
  144.                 NameFromLock(wbarg->wa_Lock, DiskObjectName, 255);
  145.                 if (DiskObjectName[strlen(DiskObjectName) - 1] != ':')
  146.                     strcat(DiskObjectName, "/");
  147.                 strcat(DiskObjectName, wbarg->wa_Name);
  148.                 toolarray = dobj->do_ToolTypes;
  149.  
  150.                 while(toolarray[i])
  151.                 {
  152.                     strcpy(args[i + 1], toolarray[i]);
  153.                     i++; argc++;
  154.                 }
  155.  
  156.                 /* Free the diskobject we got */
  157.                 FreeDiskObject(dobj);
  158.             }
  159.  
  160.             for (i = 0; i < argc; i++)
  161.                 argv[i] = (char *)&args[i];
  162.  
  163.             if (olddir != -1)
  164.                 CurrentDir(olddir);
  165.  
  166.             CloseLibrary(IconBase);
  167.             IconBase = NULL;
  168.         }
  169.         else
  170.             fprintf(conwin, "ERROR: Can't open icon.library V36+\n");
  171.  
  172.         main(argc, argv);
  173.         fclose(conwin);
  174.     }
  175. }
  176.  
  177.  
  178. void main(int argc, char **argv)
  179. {
  180.   int loop;
  181.   struct Window *oldwinptr;
  182.  
  183.   if (DiskObjectName[0] == NULL)
  184.       strcpy(DiskObjectName, argv[0]);
  185.       // strcpy(DiskObjectName, "PROGDIR:DClock");
  186.  
  187.   if (OpenAllLibs())
  188.   {
  189.       PubScreenName = myArgString(argv, "PUBSCREEN", "Workbench"); 
  190.       if (! LoadPreferences(&myPrefs))
  191.       {
  192.           rtEZRequestA("       DClock V1.6\n"
  193.                             "       ~~~~~~~~~~~\n"
  194.                             "Couldn't load preferences.\n"
  195.                             "The default values will be\n"
  196.                             "       used instead.\n",
  197.                             "Okay", NULL, NULL, ezRequestTags);
  198.       }
  199.       do
  200.       {
  201.           loop = HANDLE_DONE; 
  202.           if (! SetupScreen())
  203.           {
  204.               ezRequestTags[0].ti_Data = (ULONG)PubScreenName;
  205.  
  206.               if (AllocateParentSignals())
  207.               {
  208.                   if (! OpenDClockWindow(&myPrefs))
  209.                   {
  210.                       InitSemaphore(&draw_semaphore);
  211.                       DrawTime(DClockWnd, &myPrefs);
  212.                       parenttask = FindTask(NULL);
  213.                       childtask = CreateTask("DClockTask", 0, (APTR)DClockTask, 4000L);
  214.                       infotask = CreateTask("DClockInfo", 0, (APTR)InfoTask, 4000L);
  215.                       Delay(30);
  216.                       loop = HandleEvents();
  217.                       Signal(childtask, exitsig);
  218.                       Signal(infotask, exit_info);
  219.                       Wait(cleanupsig);
  220.                       Wait(info_done);
  221.                       Forbid();
  222.                       DeleteTask(childtask);
  223.                       DeleteTask(infotask);
  224.                       Permit();
  225.                       myPrefs.windowX = DClockWnd->LeftEdge; // Save current
  226.                       myPrefs.windowY = DClockWnd->TopEdge;  // window position
  227.                       CloseDClockWindow();
  228.                   }
  229.               }
  230.               FreeParentSignals(); /* This is safe */
  231.               CloseDownScreen();   /* Unlock screen to allow screenmode changes */
  232.  
  233.               if (loop == HANDLE_ICONIFY)
  234.               {
  235.                   if (! DoAppIcon())
  236.                       rtEZRequestA("Sorry, cannot install AppIcon.",
  237.                                         "Hmmm", NULL, NULL, ezRequestTags);
  238.  
  239.  
  240.               }
  241.           }
  242.       }
  243.       while (loop != HANDLE_DONE);
  244.   }
  245.   CloseAllLibs();  // This is safe. Function will check for valid pointers
  246. }
  247.  
  248. // --------------------------------------------------------------------------
  249.  
  250. int HandleEvents(void)
  251. {
  252.     struct Window *actWindow = DClockWnd;
  253.     struct Menu *actMenus = DClockMenus;
  254.     struct IntuiMessage *msg;
  255.     struct MenuItem *item, *it;
  256.     BOOL done = FALSE;
  257.     BOOL loop = FALSE;
  258.     UWORD menuNumber;
  259.     UWORD stripNum;
  260.     UWORD itemNum;
  261.     UWORD subNum;
  262.     ULONG Class;
  263.     UWORD Code;
  264.     ULONG signal;
  265.     ULONG windowSig = 1L << actWindow->UserPort->mp_SigBit;
  266.     int timeval;
  267.     int ret = HANDLE_DONE;
  268.  
  269.     while (! done)
  270.     {
  271.         signal = Wait(windowSig | cleanupsig | timercmd | alarmcmd);
  272.         if (! (signal & windowSig))
  273.             loop = FALSE;
  274.         else
  275.             loop = TRUE;
  276.  
  277.         if (signal & cleanupsig)
  278.         {
  279.             done = TRUE;
  280.             loop = FALSE;
  281.              rtEZRequestA("Error:\n\n"
  282.                               "Child task ended on error condition.\n"
  283.                               "Either the timer device  couldn't be\n"
  284.                               "opened or a signal allocation failed",
  285.                               "Quit", NULL, NULL, ezRequestTags);
  286.         }
  287.  
  288.         if (signal & timercmd)
  289.         {
  290.             char concmd[256];
  291.             BPTR out;
  292.             struct TagItem items[3];
  293.  
  294.             strcpy(concmd, "CON:0/0/640/200/DClock Output/AUTO/CLOSE/WAIT/SCREEN ");
  295.             strcat(concmd, PubScreenName);
  296.             out = Open(concmd, MODE_NEWFILE);
  297.  
  298.             items[0].ti_Tag = SYS_Input;  items[0].ti_Data = (ULONG)out;
  299.             items[1].ti_Tag = SYS_Output; items[1].ti_Data = 0;
  300.             items[2].ti_Tag = TAG_DONE;   items[2].ti_Data = 0;
  301.  
  302.             if (SystemTagList(myPrefs.timrcmd, items))
  303.                 rtEZRequestA("Oops, an  error occurred.\n"
  304.                                  "Check your timer command.",
  305.                                  "I'll do that", NULL, NULL, ezRequestTags);
  306.             if (out)
  307.                 Close(out);
  308.         }
  309.         if (signal & alarmcmd)
  310.         {
  311.             char concmd[256];
  312.             BPTR out;
  313.             struct TagItem items[3];
  314.  
  315.             strcpy(concmd, "CON:0/0/640/200/DClock Output/AUTO/CLOSE/WAIT/SCREEN ");
  316.             strcat(concmd, PubScreenName);
  317.             out = Open(concmd, MODE_NEWFILE);
  318.  
  319.             items[0].ti_Tag = SYS_Input;  items[0].ti_Data = (ULONG)out;
  320.             items[1].ti_Tag = SYS_Output; items[1].ti_Data = 0;
  321.             items[2].ti_Tag = TAG_DONE;   items[2].ti_Data = 0;
  322.  
  323.             if (SystemTagList(myPrefs.alrmcmd, items))
  324.                 rtEZRequestA("Oops, an  error occurred.\n"
  325.                                  "Check your alarm command.",
  326.                                  "I'll do that", NULL, NULL, ezRequestTags);
  327.             if (out)
  328.                 Close(out);
  329.         }
  330.  
  331.         while ((! done) && (loop) && (msg = GT_GetIMsg(actWindow->UserPort)))
  332.         {
  333.             Class = msg->Class;
  334.             Code = msg->Code;
  335.             GT_ReplyIMsg(msg);
  336.             switch (Class)
  337.             {
  338.                 case IDCMP_REFRESHWINDOW:
  339.                       DrawTime(DClockWnd, &myPrefs);
  340.                       break;
  341.                 case IDCMP_MENUPICK:
  342.                       menuNumber = Code;
  343.                       while ((menuNumber != MENUNULL) && (! done))
  344.                       {
  345.                           item = ItemAddress(actMenus, menuNumber);
  346.  
  347.                           stripNum = MENUNUM(menuNumber);
  348.                           itemNum = ITEMNUM(menuNumber);
  349.                           subNum = SUBNUM(menuNumber);
  350.                           switch (stripNum)
  351.                           {
  352.                               case MENU_DCLOCK:
  353.                                   switch (itemNum)
  354.                                   {
  355.                                       case MENU_SECONDS:
  356.                                           if (IsChecked(menuNumber))
  357.                                               myPrefs.seconds = TRUE;
  358.                                           else
  359.                                               myPrefs.seconds = FALSE;
  360.                                           DrawTime(DClockWnd, &myPrefs);
  361.                                           break;
  362.                                       case MENU_24H:
  363.                                           if (IsChecked(menuNumber))
  364.                                               myPrefs.twentyfour = TRUE;
  365.                                           else
  366.                                               myPrefs.twentyfour = FALSE;
  367.                                           DrawTime(DClockWnd, &myPrefs);
  368.                                           break;
  369.                                       case MENU_DATE:
  370.                                           if (IsChecked(menuNumber))
  371.                                               myPrefs.date = TRUE;
  372.                                           else
  373.                                               myPrefs.date = FALSE;
  374.                                           ret = HANDLE_DATE;
  375.                                           done = TRUE;
  376.                                           loop = FALSE;
  377.                                           break;
  378.                                       case MENU_DDMMYY:
  379.                                           if (IsChecked(menuNumber))
  380.                                               myPrefs.ddmmyy = TRUE;
  381.                                           else
  382.                                               myPrefs.ddmmyy = FALSE;
  383.                                           DrawTime(DClockWnd, &myPrefs);
  384.                                           break;
  385.                                       case MENU_ALARMON:
  386.                                           if (IsChecked(menuNumber))
  387.                                           {
  388.                                               myPrefs.alarm = TRUE;
  389.                                               DrawAlarm(myPrefs.foreground);
  390.                                           }
  391.                                           else
  392.                                           {
  393.                                               myPrefs.alarm = FALSE;
  394.                                               EraseAlarm(myPrefs.background);
  395.                                           }
  396.                                           break;
  397.                                       case MENU_TIMERON:
  398.                                           if (IsChecked(menuNumber))
  399.                                           {
  400.                                               myPrefs.timer = TRUE;
  401.                                               DrawTimer(myPrefs.foreground);
  402.                                           }
  403.                                           else
  404.                                           {
  405.                                               myPrefs.timer = FALSE;
  406.                                               EraseTimer(myPrefs.background);
  407.                                           }
  408.                                           break;
  409.                                       case MENU_SETALARM:
  410.                                           timeval = SetAlarm(SET_ALARM, myPrefs.alarmval);
  411.                                           if (timeval != SET_CANCEL)
  412.                                           {
  413.                                               myPrefs.alarmval = timeval;
  414.                                               it = (struct MenuItem *)ItemAddress(DClockMenus, FULLMENUNUM(MENU_DCLOCK, MENU_ALARMON, 0));
  415.                                               ClearMenuStrip(DClockWnd);
  416.                                               it->Flags = it->Flags | (CHECKED);
  417.                                               SetMenuStrip(DClockWnd, DClockMenus);
  418.                                               myPrefs.alarm = TRUE;
  419.                                               DrawAlarm(myPrefs.foreground);
  420.                                           }
  421.                                           break;
  422.                                       case MENU_SETTIMER:
  423.                                           timeval = SetAlarm(SET_TIMER, Seconds2Timeval(myPrefs.timerval));
  424.                                           if (timeval != SET_CANCEL)
  425.                                           {
  426.                                               myPrefs.timerval = Timeval2Seconds(timeval);
  427.                                               if (myPrefs.timerval > 0)
  428.                                               {
  429.                                                   it = (struct MenuItem *)ItemAddress(DClockMenus, FULLMENUNUM(MENU_DCLOCK, MENU_TIMERON, 0));
  430.                                                   ClearMenuStrip(DClockWnd);
  431.                                                   it->Flags = it->Flags | (CHECKED);
  432.                                                   SetMenuStrip(DClockWnd, DClockMenus);
  433.                                                   OnMenu(DClockWnd, FULLMENUNUM(MENU_DCLOCK, MENU_TIMERON, 0));
  434.                                                   myPrefs.timer = TRUE;
  435.                                                   DrawTimer(myPrefs.foreground);
  436.                                               }
  437.                                               else
  438.                                                   TurnTimerOff(&myPrefs);
  439.                                           }
  440.                                           break;
  441.                                       case MENU_ALARMCMD:
  442.                                           SetCommand(ALARM);
  443.                                           break;
  444.                                       case MENU_TIMERCMD:
  445.                                           SetCommand(TIMER);
  446.                                           break;
  447.                                       case MENU_SAVEPREFS:
  448.                                           if (! SavePreferences(&myPrefs))
  449.                                           {
  450.                                               rtEZRequestA("          DClock V1.6\n"
  451.                                                                 "          ~~~~~~~~~~~\n"
  452.                                                                 "Couldn't save preferences to ENVARC:\n",
  453.                                                                 "Okay", NULL, NULL, ezRequestTags);
  454.                                           }
  455.                                           break;
  456.                                       case MENU_ABOUT:
  457.                                           About();
  458.                                           break;
  459.                                       case MENU_QUIT:
  460.                                           ret = HANDLE_DONE;
  461.                                           done = TRUE;
  462.                                           loop = FALSE;
  463.                                           break;
  464.                                   }
  465.                                   break;
  466.                               case MENU_WINDOW:
  467.                                   switch (itemNum)
  468.                                   {
  469.                                       case MENU_FOREGROUND:
  470.                                           myPrefs.foreground = subNum;
  471.                                           DrawTime(DClockWnd, &myPrefs);
  472.                                           break;
  473.                                       case MENU_BACKGROUND:
  474.                                           myPrefs.background = subNum;
  475.                                           DrawTime(DClockWnd, &myPrefs);
  476.                                           break;
  477.                                       case MENU_DRAGBAR:
  478.                                           if (IsChecked(menuNumber))
  479.                                               myPrefs.dragbar = TRUE;
  480.                                           else
  481.                                               myPrefs.dragbar = FALSE;
  482.                                           ret = HANDLE_DRAGBAR;
  483.                                           done = TRUE;
  484.                                           loop = FALSE;
  485.                                           break;
  486.                                       case MENU_ACTIVATE:
  487.                                           if (IsChecked(menuNumber))
  488.                                               myPrefs.activate = TRUE;
  489.                                           else
  490.                                               myPrefs.activate = FALSE;
  491.                                           break;
  492.                                       case MENU_ICONIFY:
  493.                                           ret = HANDLE_ICONIFY;
  494.                                           done = TRUE;
  495.                                           loop = FALSE;
  496.                                           break;
  497.                                   }
  498.                                   break;
  499.                           }
  500.                           menuNumber = item->NextSelect;
  501.                       }
  502.             }
  503.         }
  504.     }
  505.     while (msg = GT_GetIMsg(actWindow->UserPort)) ;
  506.     return ret;
  507. }
  508.  
  509. // --------------------------------------------------------------------------
  510.  
  511. BOOL OpenAllLibs(void)
  512. {
  513.     ReqToolsBase = (struct ReqToolsBase *)OpenLibrary(REQTOOLSNAME,REQTOOLSVERSION);
  514.     if (ReqToolsBase)
  515.     {
  516.         IntuitionBase = (struct IntuitionBase *)ReqToolsBase->IntuitionBase;
  517.         GadToolsBase = (struct Library *)ReqToolsBase->GadToolsBase;
  518.         GfxBase = (struct GfxBase *)ReqToolsBase->GfxBase;
  519.     }
  520.     IconBase = OpenLibrary("icon.library", 36L);
  521.     WorkbenchBase = OpenLibrary("workbench.library", 37L);
  522.  
  523.     return (ReqToolsBase && IconBase && WorkbenchBase);
  524. }
  525.  
  526. void CloseAllLibs(void)
  527. {
  528.     if (ReqToolsBase)
  529.         CloseLibrary((struct Library *)ReqToolsBase);
  530.     if (IconBase)
  531.         CloseLibrary(IconBase);
  532.     if (WorkbenchBase)
  533.         CloseLibrary(WorkbenchBase);
  534. }
  535.  
  536. // --------------------------------------------------------------------------
  537.  
  538. void About(void)
  539. {
  540.     rtEZRequestA("        DClock V1.6\n"
  541.                      "        ~~~~~~~~~~~\n"
  542.                      "   © 1995 by  Karl Lukas\n\n"
  543.                      "    Hinterzweienstr. 91\n"
  544.                      "      CH-4132 Muttenz\n"
  545.                      "        Switzerland\n\n"
  546.                      "Email:\n\n"
  547.                      "  kalle@gis.geogr.unizh.ch",
  548.                      "Okay", NULL, NULL, ezRequestTags);
  549. }
  550.  
  551. // --------------------------------------------------------------------------
  552.  
  553. BOOL LoadPreferences(struct AppPrefs *prefs)
  554. {
  555.     BPTR phandle = NULL;
  556.     if (phandle = Open(PrefsName, MODE_OLDFILE))
  557.     {
  558.         LONG len = Read(phandle, prefs, sizeof (AppPrefs));
  559.         Close(phandle);
  560.  
  561.         myPrefs.timer = FALSE;
  562.         myPrefs.timerval = 0;
  563.         if (len != sizeof (AppPrefs))
  564.             return FALSE;
  565.         else
  566.             return TRUE;
  567.     }
  568.     else
  569.         return FALSE;
  570. }
  571.  
  572. BOOL SavePreferences(struct AppPrefs *prefs)
  573. {
  574.     BPTR phandle = NULL;
  575.  
  576.     prefs->windowX = DClockWnd->LeftEdge;
  577.     prefs->windowY = DClockWnd->TopEdge;
  578.     if (phandle = Open(PrefsName, MODE_NEWFILE))
  579.     {
  580.         LONG len = Write(phandle, prefs, sizeof (AppPrefs));
  581.         Close(phandle);
  582.         if (len != sizeof (AppPrefs))
  583.             return FALSE;
  584.         else
  585.             return TRUE;
  586.     }
  587.     else
  588.         return FALSE;
  589. }
  590.  
  591. // --------------------------------------------------------------------------
  592.  
  593. BOOL OpenTimerDevice(void)
  594. {
  595.     if (TimerMP = CreatePort(0,0))
  596.     {
  597.         if (TimerIO = (struct timerequest *)
  598.                           CreateExtIO(TimerMP, sizeof(struct timerequest)))
  599.         {
  600.             if (! (timerError = OpenDevice(TIMERNAME, UNIT_VBLANK,
  601.                                              (struct IORequest *)TimerIO, 0L)))
  602.             {
  603.                 SetTimer(1);  // Delay min. 1 second
  604.                 return TRUE;
  605.             }
  606.         }
  607.  
  608.     }
  609.     return FALSE;
  610. }
  611.  
  612. void CloseTimerDevice(void)
  613. {
  614.     if (! timerError)
  615.     {
  616.         if (! (CheckIO((struct IORequest *)TimerIO)))
  617.         {
  618.             AbortIO((struct IORequest *)TimerIO);
  619.         }
  620.         WaitIO((struct IORequest *)TimerIO);
  621.         CloseDevice((struct IORequest *)TimerIO);
  622.     }
  623.     if (TimerIO)
  624.         DeleteExtIO((struct IORequest *)TimerIO);
  625.     if (TimerMP)
  626.         DeletePort(TimerMP);
  627. }
  628.  
  629. void SetTimer(ULONG delay)
  630. {
  631.     TimerIO->tr_time.tv_secs = delay;     // Set delay
  632.     TimerIO->tr_time.tv_micro = 0;
  633.     TimerIO->tr_node.io_Command = TR_ADDREQUEST;
  634.     SendIO((struct IORequest *)TimerIO);
  635. }
  636.  
  637. // --------------------------------------------------------------------------
  638.  
  639. int Timeval2Seconds(int timeval)
  640. {
  641.     int hours, mins, seconds;
  642.  
  643.     hours = timeval / 10000;
  644.     mins = (timeval - hours * 10000) / 100;
  645.     seconds = (timeval - hours * 10000 - mins * 100);
  646.  
  647.     return hours * 3600 + mins * 60 + seconds;
  648. }
  649.  
  650. int Seconds2Timeval(int secs)
  651. {
  652.     int hours, mins, seconds;
  653.  
  654.     hours = secs / 3600;
  655.     mins = (secs - hours * 3600) / 60;
  656.     seconds = (secs - hours * 3600 - mins * 60);
  657.  
  658.     return hours * 10000 + mins * 100 + seconds;
  659. }
  660.  
  661. // --------------------------------------------------------------------------
  662.  
  663. BOOL IsChecked(int menuNumber)
  664. {
  665.     struct MenuItem *it;
  666.  
  667.     it = (struct MenuItem *)ItemAddress(DClockMenus, menuNumber);
  668.     if (it->Flags & CHECKED)
  669.         return TRUE;
  670.     else
  671.         return FALSE;
  672. }
  673.  
  674.  
  675. // --------------------------------------------------------------------------
  676.  
  677. BOOL AllocateParentSignals(void) /* This is called from parent process */
  678. {
  679.     BOOL ret = TRUE;
  680.  
  681.  
  682.     if ((cleanupsignum = AllocSignal(-1)) == -1)  /* Allocate cleanupsig */
  683.         ret = FALSE;
  684.     else
  685.         cleanupsig = 1L << cleanupsignum;
  686.  
  687.     if ((info_done_num = AllocSignal(-1)) == -1)  /* Allocate info_done */
  688.         ret = FALSE;
  689.     else
  690.         info_done = 1L << info_done_num;
  691.  
  692.     if ((timercmdnum = AllocSignal(-1)) == -1)  /* Allocate timercmd */
  693.         ret = FALSE;
  694.     else
  695.         timercmd = 1L << timercmdnum;
  696.  
  697.     if ((alarmcmdnum = AllocSignal(-1)) == -1)  /* Allocate alarmcmd */
  698.         ret = FALSE;
  699.     else
  700.         alarmcmd = 1L << alarmcmdnum;
  701.  
  702.     return ret;
  703. }
  704.  
  705.  
  706. void FreeParentSignals(void)
  707. {
  708.     if (info_done_num != -1)
  709.         FreeSignal(info_done_num);
  710.     if (cleanupsignum != -1)
  711.         FreeSignal(cleanupsignum);
  712.     if (timercmdnum != -1)
  713.         FreeSignal(timercmdnum);
  714.     if (alarmcmdnum != -1)
  715.         FreeSignal(alarmcmdnum);
  716. }
  717.  
  718.  
  719. BOOL AllocateChildSignals(void) /* This is called from child process */
  720. {
  721.     BOOL ret = TRUE;
  722.  
  723.     if (exitsignum = AllocSignal(-1) == -1)  /* Allocate exitsig */
  724.         ret = FALSE;
  725.     else
  726.         exitsig = 1L << exitsignum;
  727.  
  728.     return ret;
  729. }
  730.  
  731. void FreeChildSignals(void)
  732. {
  733.     if (exitsignum != -1)
  734.         FreeSignal(exitsignum);
  735. }
  736.  
  737. BOOL AllocateInfoSignals(void)
  738. {
  739.     BOOL ret = TRUE;
  740.  
  741.     if ((alarmsignum = AllocSignal(-1)) == -1)  /* Allocate alarmsig */
  742.         ret = FALSE;
  743.     else
  744.         alarmsig = 1L << alarmsignum;
  745.  
  746.     if ((timersignum = AllocSignal(-1)) == -1)  /* Allocate timersig */
  747.         ret = FALSE;
  748.     else
  749.         timersig = 1L << timersignum;
  750.  
  751.     if ((exit_info_num = AllocSignal(-1)) == -1)
  752.         ret = FALSE;
  753.     else
  754.         exit_info = 1L << exit_info_num;
  755.  
  756.     return ret;
  757. }
  758.  
  759. void FreeInfoSignals(void)
  760. {
  761.     if (alarmsignum != -1)
  762.         FreeSignal(alarmsignum);
  763.     if (timersignum != -1)
  764.         FreeSignal(timersignum);
  765.     if (exit_info_num != -1)
  766.         FreeSignal(exit_info_num);
  767. }
  768.  
  769.  
  770. // --------------------------------------------------------------------------
  771.  
  772.  
  773.  
  774. void DClockTask(void)
  775. {
  776.     ULONG signal;
  777.     ULONG timerSig;
  778.     BOOL done = FALSE;
  779.  
  780.     if (OpenTimerDevice())
  781.     {
  782.         if (AllocateChildSignals())
  783.         {
  784.             timerSig = 1L << TimerMP->mp_SigBit;
  785.             while (! done)
  786.             {
  787.                 signal = Wait(timerSig | exitsig);
  788.                 if (signal & timerSig)
  789.                 {
  790.                     TimerMSG = GetMsg(TimerMP);
  791.                     SetTimer(1);
  792.                     RedrawTime(DClockWnd, &myPrefs);
  793.                 }
  794.                 if (signal & exitsig)
  795.                     done = TRUE;
  796.             }
  797.         }
  798.         FreeChildSignals(); /* This is safe */
  799.         CloseTimerDevice();
  800.     }
  801.     Signal(parenttask, cleanupsig);
  802.     Wait(0);
  803. }
  804.  
  805.  
  806.  
  807. void InfoTask(void)
  808. {
  809.     ULONG signal;
  810.     BOOL done = FALSE;
  811.  
  812.     if (AllocateInfoSignals())
  813.     {
  814.         while (! done)
  815.         {
  816.             signal = Wait(alarmsig | timersig | exit_info);
  817.             if (signal & alarmsig)
  818.             {
  819.                 if (myPrefs.alrmcmd[0] != 0)
  820.                     Signal(parenttask, alarmcmd);
  821.                 ShowAlarm(&myPrefs);
  822.             }
  823.             if (signal & timersig)
  824.             {
  825.                 if (myPrefs.timrcmd[0] != 0)
  826.                     Signal(parenttask, timercmd);
  827.                 ShowTimer(&myPrefs);
  828.             }
  829.             if (signal & exit_info)
  830.                 done = TRUE;
  831.         }
  832.     }
  833.     FreeInfoSignals(); /* This is safe */
  834.     Signal(parenttask, info_done);
  835.     Wait(0);
  836. }
  837.  
  838. // ----------------------------------------------------------------------
  839.  
  840. void SetCommand(int type)
  841. {
  842.     char buf[128];
  843.     char buf2[32];
  844.  
  845.     struct TagItem tags[] =
  846.     {
  847.         RTGS_AllowEmpty, TRUE,
  848.         RT_PubScrName, (ULONG)PubScreenName,
  849.         TAG_DONE, 0
  850.     };
  851.  
  852.     if (type == ALARM)
  853.     {
  854.         strcpy(buf2, "Alarm: ");
  855.         strcpy(buf, myPrefs.alrmcmd);
  856.     }
  857.     else if (type == TIMER)
  858.     {
  859.         strcpy(buf2, "Timer: ");
  860.         strcpy(buf, myPrefs.timrcmd);
  861.     }
  862.     strcat(buf2, "Enter command string");
  863.  
  864.     if (rtGetStringA((UBYTE *)buf, 128, buf2, NULL, tags))
  865.     {
  866.         if (type == ALARM)
  867.             strcpy(myPrefs.alrmcmd, buf);
  868.         else if (type == TIMER)
  869.             strcpy(myPrefs.timrcmd, buf);
  870.     }
  871. }
  872.  
  873. // ----------------------------------------------------------------------------
  874.  
  875. int myArgInt(char **tooltypes, char *tool, int dfault)
  876. {
  877.     int j, i = 0;
  878.     char *tval;
  879.  
  880.     if (tval = (char *)FindToolType((UBYTE **)tooltypes, tool))
  881.         return atoi(tval);
  882.     else
  883.         return dfault;
  884. }
  885.  
  886. char *myArgString(char **tooltypes, char *tool, char *dfault)
  887. {
  888.     int j, i = 0;
  889.     char *tval;
  890.  
  891.     if (tval = (char *)FindToolType((UBYTE **)tooltypes, tool))
  892.         return tval;
  893.     else
  894.         return dfault;
  895. }
  896.  
  897. BOOL myArgBool(char **tooltypes, char *tool, BOOL dfault)
  898. {
  899.     int j, i = 0;
  900.     char *tval;
  901.  
  902.     if (tval = (char *)FindToolType((UBYTE **)tooltypes, tool))
  903.     {
  904.         if (*tval == 'n' || *tval == 'N')
  905.             return FALSE;
  906.         if (*tval == 'y' || *tval == 'Y')
  907.             return TRUE;
  908.         /* else: Error, wrong argument */
  909.     }
  910.     return dfault;
  911. }
  912.  
  913. // ----------------------------------------------------------------------------
  914.  
  915. BOOL DoAppIcon(void)
  916. {
  917.     struct DiskObject   *dobj = NULL;
  918.     struct MsgPort   *msgport = NULL;
  919.     struct AppIcon   *appicon = NULL;
  920.     struct AppMessage *appmsg = NULL;
  921.     BOOL ret = FALSE;
  922.  
  923.     if (dobj = GetDiskObjectNew(DiskObjectName))
  924.     {
  925.         dobj->do_Type = NULL;
  926.         if (msgport = CreateMsgPort())
  927.         {
  928.             if (appicon = AddAppIconA(0l, 0L, "DClock", msgport, NULL, dobj, NULL))
  929.             {
  930.                 WaitPort(msgport);
  931.                 RemoveAppIcon(appicon); 
  932.                 while(appmsg = (struct AppMessage *)GetMsg(msgport))
  933.                     ReplyMsg((struct Message *)appmsg);
  934.                 ret = TRUE;
  935.             }
  936.             DeleteMsgPort(msgport);
  937.         }
  938.         FreeDiskObject(dobj);
  939.     }
  940.     return ret;
  941. }
  942.  
  943.